﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class InitConfig : MonoBehaviour
{
    public static InitConfig instance;
    public Dictionary<string, RankConfigDataStyle> RankConfigDictionary = new Dictionary<string, RankConfigDataStyle>();
    public Dictionary<string, ReceConfigDataStyle> ReceConfigDictionary = new Dictionary<string, ReceConfigDataStyle>();
    private int RankConfigFirstId;
    private int RankConfigLastId;
    // Use this for initialization
    void Awake()
    {
        instance = this;
        RankConfigRead();
        ReceConfigRead();
    }
    void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		
	}
    public class RankConfigDataStyle
    {
        public string rank;
        public string name;
        public string grade;
        public string headRes;
    }
    public class ReceConfigDataStyle
    {
        public string problem;
        public string answer;
        public string[] disturb;
    }
    private void RankConfigRead()
    {
        TextAsset binAsset = Resources.Load( "config/Ranking", typeof( TextAsset ) ) as TextAsset;
        //获取配置表的行
        string[] configdata = binAsset.text.Split( '\n' );

        foreach( var data in configdata )
        {
            RankConfigDataStyle configdat = new RankConfigDataStyle();
            string[] dath = data.ToString().Split( ',' );
            if( dath.Length >= 4 )
            {
                string id = dath[0];
                configdat.rank = dath[1];
                configdat.name = dath[2];
                configdat.grade = dath[3];
                configdat.headRes = dath[4].Substring( 0, dath[4].Length - 1 );
                RankConfigDictionary.Add( id, configdat );
            }
        }
        RankConfigLastId = int.Parse( configdata[configdata.Length - 1].Split( ',' )[0] );  //最后一个ID
        RankConfigFirstId = int.Parse( configdata[1].Split( ',' )[0] );  //第一个id        
    }
    private void ReceConfigRead()
    {
        TextAsset binAsset = Resources.Load( "config/ReceProblem", typeof( TextAsset ) ) as TextAsset;
        string[] configdata = binAsset.text.Split( '\n' );        
        foreach( var data in configdata )
        {
            ReceConfigDataStyle configdat = new ReceConfigDataStyle();
            string[] dath = data.ToString().Split( ',' );
            if (dath.Length < 5)
                return;
            string id = dath[0];
            configdat.problem = dath[1];
            configdat.answer = dath[2];
            if (dath.Length >= 5 & dath.Length < 6)
            {
                configdat.disturb = new string[] { dath[3], dath[4] };                
            }
            else if (dath.Length >= 6 & dath.Length < 7)
            {
                configdat.disturb = new string[] { dath[3], dath[4], dath[5] };
            }
            else if (dath.Length >= 7)
            {
                configdat.disturb = new string[] { dath[3], dath[4], dath[5], dath[6] };
            }
            ReceConfigDictionary.Add( id, configdat );
        }
    }
}
